/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/comm",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",
    "scprit/followup/app/lostFollowupManager/periodPatient.ui.js"],function($,$bb,$comm,$datatableServers,$datatableUi,$periodPatientUi){
	var servers = {};
	var URL = $bb.baseUrl("followup");

    /**
     * 处理随访提醒参数
     */
    var getParamsFollowupWarn = function(data){
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var param = {
            followQueueId: data.followQueueId,
            patientId: data.patientId,
            taskId: data.taskId,
        };
        var informInfo = {
            followQueueId: data.followQueueId, 
            informType: "2",
            patientId: data.patientId,
            ptSchemeId: data.patientSchemeId, 
            taskId: data.taskId, 
            taskName: data.taskName,
        };  
        var oprInfo = {
            opCode: "1015",
            opNote: "【"+doctorName + "】医生给【"+ data.patientName +"】患者发送的" + data.taskName + "提醒",
            opType: "2",
        };  
        var param2 = {};
        param2.informInfo = informInfo;
        param2.oprInfo = oprInfo;
        return {
            param: param,
            param2: param2,
        };
    }
    /**
     * 请求随访任务提醒列表
     */
    var ajaxWarns = function(params,callBack){
        $bb.ajax({
            url: URL + "followup/service/patient/inform/ptInformScheduleQuery",
            data: JSON.stringify({
                data: params.param
            }),
            success: function (data) {
                if(data.success){
                    callBack(data.data,params.param2);
                }else{
                    if(data.code == "2013010002"){
                        $bb.toastConfirmShow(ajaxWarnSend,params.param2,"科室无随访排班，是否发送提醒");
                    }else{
                        $bb.toastShow(data.msg ? data.msg : "科室无随访排班，无法发送提醒");
                    }
                }
            }
        });
    }
    /**
     * 发送随访提醒：排班计划及门诊地址列表
     */
    var ajaxWarnSend = function(param,className){
        $bb.ajax({
            url: URL + "followup/service/patient/inform/ptWarnInformDeal",
            data: JSON.stringify({
                data: param
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("发送成功");
                }else{
                    $bb.toastShow(data.msg ? data.msg : "发送失败");
                }
                if(className)$("." + className).modal('hide');
            }
        });
    }
    /**
     * 点击“发送提醒”后的事件函数
     */
    var onWarnSendSelect = function(){
        var param = $(this).data("param");
        var className = $(this).data("className");
        $.extend(param.informInfo,{
            orderAddress: $("#orderAddress").val(),
            orderDate: $("#orderDate").val(), 
        });
        ajaxWarnSend(param,className);
    }
    /**
     * 请求随访任务提醒列表后的回调函数
     */
    var ajaxWarnsCallBack = function(data,param){
        var className = "followupwarnModal";
        $comm.modal({
            "className": className,
            "tit": "随访任务提醒",
        },function(){
            $periodPatientUi.followupWarnInit({
                dom: $("." + className + " .modal-body"),
                list: data,
            });
            $("." + className).modal('show');
            $("#followupWarnSend").unbind("click");
            $("#followupWarnSend").data({"param":param,"className":className});
            $("#followupWarnSend").click(onWarnSendSelect);
        });  
    }
    /**
     * 入参
     */
    var inParams = function(args){
        var ageFrom, ageTo;
        if($("#ageFrom-year").val() !="" || $("#ageFrom-month").val() !=""){
            ageFrom = parseInt($("#ageFrom-year").val()*12) + parseInt($("#ageFrom-month").val()*1);
        }else{
            ageFrom = "";
        }
        if($("#ageTo-year").val() !="" || $("#ageTo-month").val() !=""){
            ageTo = parseInt($("#ageTo-year").val()*12) + parseInt($("#ageTo-month").val()*1);
        }else{
            ageTo = "";
        }
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "patientName": $.trim($("#patientName").val()),
                "phoneNum": $.trim($("#phoneNum").val()),
                "hisNo": $.trim($("#hisNo").val()),
                "ageFrom": ageFrom,
                "ageTo": ageTo,
                "qryEndTime": $("#timeTo").val(),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : e.hisNo3 ? e.hisNo3 : "-";
    }
    var columnsPatientName = function(e){
        var imgSrc = 'page/followup/imgs/comm/';
        imgSrc += e.gender == 'F' ? 'girl.png' : 'boy.png';
        var h = '<img class="pic-sex" src='+imgSrc+'>';
            h += '<span class="patient-name">' + e.patientName + '</span>'; 
        return h;
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":columnsHisNo},
            {"data":columnsPatientName},
            {"data":'phoneNum'},
            {
                "data":'birthday',
                "className":"birthday"
            },
            {
                "data":'age',
                "className":"age"
            },
            {
                "data":'pregnancyWeek',
                "className":"pregnancyWeek"
            },
            {"data":'endTime'},
            {"data":'taskName'},
            {"data":'completeNum'},
            {"data":'lostNum'},
            {"data":'followQueueName'},
            {
                "data": null,
                "className":"operation"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = '<div class="btn-look">提醒</div>';
        $('td.operation', row).html(h);
        $("td.operation .btn-look",row).unbind("click");
        $("td.operation .btn-look",row).click(function(){
            var params = getParamsFollowupWarn(data);
            ajaxWarns(params,ajaxWarnsCallBack);
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#periodPatientList").prev("div.row").remove();
        $("#serchBtn").click(function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
        $("#resetBtn").click(function(){
            $("#patientName,#phoneNum,#hisNo,#timeTo,#ageFrom-year,#ageFrom-month,#ageTo-year,#ageTo-month").val("");
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/ptWarnQueueListQuery?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});